package route

import (
	"testing"
	"time"

	"github.com/yusing/godoxy/internal/common"
	route "github.com/yusing/godoxy/internal/route/types"
	"github.com/yusing/godoxy/internal/types"
	expect "github.com/yusing/goutils/testing"
)

func TestRouteValidate(t *testing.T) {
	t.Run("ReservedPort", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeHTTP,
			Host:   "localhost",
			Port:   route.Port{Proxy: common.ProxyHTTPPort},
		}
		err := r.Validate()
		expect.HasError(t, err, "Validate should return error for localhost with reserved port")
		expect.ErrorContains(t, err, "reserved for godoxy")
	})

	t.Run("ListeningPortWithHTTP", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeHTTP,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80, Listening: 1234},
		}
		err := r.Validate()
		expect.HasError(t, err, "Validate should return error for HTTP scheme with listening port")
		expect.ErrorContains(t, err, "unexpected listening port")
	})

	t.Run("DisabledHealthCheckWithLoadBalancer", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeHTTP,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80},
			HealthCheck: types.HealthCheckConfig{
				Disable: true,
			},
			LoadBalance: &types.LoadBalancerConfig{
				Link: "test-link",
			}, // Minimal LoadBalance config with non-empty Link will be checked by UseLoadBalance
		}
		err := r.Validate()
		expect.HasError(t, err, "Validate should return error for disabled healthcheck with loadbalancer")
		expect.ErrorContains(t, err, "cannot disable healthcheck")
	})

	t.Run("FileServerScheme", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeFileServer,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80},
			Root:   "/tmp", // Root is required for file server
		}
		err := r.Validate()
		expect.NoError(t, err, "Validate should not return error for valid file server route")
		expect.NotNil(t, r.impl, "Impl should be initialized")
	})

	t.Run("HTTPScheme", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeHTTP,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80},
		}
		err := r.Validate()
		expect.NoError(t, err, "Validate should not return error for valid HTTP route")
		expect.NotNil(t, r.impl, "Impl should be initialized")
	})

	t.Run("TCPScheme", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeTCP,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80, Listening: 8080},
		}
		err := r.Validate()
		expect.NoError(t, err, "Validate should not return error for valid TCP route")
		expect.NotNil(t, r.impl, "Impl should be initialized")
	})

	t.Run("DockerContainer", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeHTTP,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80},
			Metadata: Metadata{
				Container: &types.Container{
					ContainerID: "test-id",
					Image: &types.ContainerImage{
						Name: "test-image",
					},
				},
			},
		}
		err := r.Validate()
		expect.NoError(t, err, "Validate should not return error for valid docker container route")
		expect.NotNil(t, r.ProxyURL, "ProxyURL should be set")
	})

	t.Run("InvalidScheme", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: 123,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80},
		}
		expect.Panics(t, func() {
			_ = r.Validate()
		}, "Validate should panic for invalid scheme")
	})

	t.Run("ModifiedFields", func(t *testing.T) {
		r := &Route{
			Alias:  "test",
			Scheme: route.SchemeHTTP,
			Host:   "example.com",
			Port:   route.Port{Proxy: 80},
		}
		err := r.Validate()
		expect.NoError(t, err)
		expect.NotNil(t, r.ProxyURL)
		expect.NotNil(t, r.HealthCheck)
	})
}

func TestPreferredPort(t *testing.T) {
	ports := types.PortMapping{
		22:   {PrivatePort: 22},
		1000: {PrivatePort: 1000},
		3000: {PrivatePort: 80},
	}

	port := preferredPort(ports)
	expect.Equal(t, port, 3000)
}

func TestDockerRouteDisallowAgent(t *testing.T) {
	r := &Route{
		Alias:  "test",
		Scheme: route.SchemeHTTP,
		Host:   "example.com",
		Port:   route.Port{Proxy: 80},
		Agent:  "test-agent",
		Metadata: Metadata{
			Container: &types.Container{
				ContainerID: "test-id",
				Image: &types.ContainerImage{
					Name: "test-image",
				},
			},
		},
	}
	err := r.Validate()
	expect.HasError(t, err, "Validate should return error for docker route with agent")
	expect.ErrorContains(t, err, "specifying agent is not allowed for docker container routes")
}

func TestRouteAgent(t *testing.T) {
	r := &Route{
		Alias:  "test",
		Scheme: route.SchemeHTTP,
		Host:   "example.com",
		Port:   route.Port{Proxy: 80},
		Agent:  "test-agent",
	}
	err := r.Validate()
	expect.NoError(t, err, "Validate should not return error for valid route with agent")
	expect.NotNil(t, r.GetAgent(), "GetAgent should return agent")
}

func TestRouteApplyingHealthCheckDefaults(t *testing.T) {
	hc := types.HealthCheckConfig{}
	hc.ApplyDefaults(types.HealthCheckConfig{
		Interval: 15 * time.Second,
		Timeout:  10 * time.Second,
	})

	expect.Equal(t, hc.Interval, 15*time.Second)
	expect.Equal(t, hc.Timeout, 10*time.Second)
}
